﻿using AutoMapper;
using HuiLian.Common.Input;
using HuiLian.Common.Output;
using HuiLian.Model.Platform;
using HuiLian.Repository.Platform.HdlScadaAlarmHistory;
using HuiLian.Service.HdlScada.HdlScadaAlarmHistory.Output;
using HuiLian.Service.HdlScada.HdlScadaAlarmHistory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HuiLian.Repository.Platform.HdlScadaSensorInfo;
using HuiLian.Service.HdlScada.HdlScadaSensorInfo.Output;
using Microsoft.AspNetCore.Mvc;
using HuiLian.Service.Platform.AlarmSchemeSet.Output;
using HuiLian.Repository.Platform.HdlScadaStationInfo;

namespace HuiLian.Service.HdlScada.HdlScadaSensorInfo
{
    public class HdlScadaSensorInfoService : IHdlScadaSensorInfoService
    {
        private readonly IMapper _mapper;
        private readonly IHdlScadaSensorInfoRepository _hdlScadaSensorInfoRepository;
        private readonly IHdlScadaStationInfoRepository _hdlScadaStationInfoRepository;

        public HdlScadaSensorInfoService(
            IMapper mapper, 
            IHdlScadaSensorInfoRepository hdlScadaSensorInfoRepository,
            IHdlScadaStationInfoRepository hdlScadaStationInfoRepository)
        {
            _mapper = mapper;
            _hdlScadaSensorInfoRepository = hdlScadaSensorInfoRepository;
            _hdlScadaStationInfoRepository = hdlScadaStationInfoRepository;
        }

        /// <summary>
        /// 获取实时报警记录
        /// </summary>
        /// <param name="sensor_code">传感器编码</param>
        /// <returns></returns>
        public async Task<IResponseOutput> GetAsync(string sensor_code)
        {
            var result = await _hdlScadaSensorInfoRepository.Select.Where(p => p.sensor_code == sensor_code).FirstAsync();
            return ResponseOutput.Ok(result);
        }

        /// <summary>
        /// 查询调用时间表信息
        /// </summary>
        /// <param name="input">分页信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> PageAsync(PageInput<HdlScadaSensorInfoEntity> input)
        {
            //var key = input.Filter?.Label;

            var list = await _hdlScadaSensorInfoRepository.Select
            .WhereDynamicFilter(input.DynamicFilter)
            //.WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
            .Count(out var total)
            .OrderByDescending(true, c => c.sensor_order)
            .Page(input.CurrentPage, input.PageSize)
            .ToListAsync(p => new HdlScadaSensorInfoListOutput {  });

            var data = new PageOutput<HdlScadaSensorInfoListOutput>()
            {
                List = list,
                Total = total
            };

            return ResponseOutput.Ok(data);
        }

        /// <summary>
        /// 通过站点名称模糊查询列表
        /// </summary>
        /// <param name="station_name">站点名称</param>
        /// <returns></returns>
        public async Task<IResponseOutput> GetStationNameListAsync(string station_name)
        {
            var list = await _hdlScadaStationInfoRepository.Select
            .WhereIf(!string.IsNullOrEmpty(station_name) ,p => p.name.Contains(station_name))
            //.WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))            
            .ToListAsync(p => new GetStationNameList { Station_code = p.code, station_name = p.name });

            return ResponseOutput.Ok(list);
        }

        /// <summary>
        /// 通过传感器名称模糊查询列表
        /// </summary>
        /// <param name="station_code">站点code</param>
        /// <param name="sensor_name">传感器名称</param>
        /// <returns></returns>
        public async Task<IResponseOutput> GetSensorNameListByStationCodeAsync(string station_code, string sensor_name)
        {
            var list = await _hdlScadaSensorInfoRepository.Select           
            .Where(p => p.station_code == station_code)
            .WhereIf(!string.IsNullOrWhiteSpace(sensor_name), p => p.sensor_name.Contains(sensor_name))
            //.WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))            
            .ToListAsync(p => new GetSensorNameListByStationNameOutput { });

            return ResponseOutput.Ok(list);
        }
    }
}
